package com.pandora.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.jdom2.Document;
import org.jdom2.JDOMException;
import org.jdom2.input.SAXBuilder;
import org.jdom2.output.Format;
import org.jdom2.output.XMLOutputter;

import com.pandora.component.DomHandler;
import com.pandora.component.SaxHandler;
import com.pandora.exception.UtilException;

public class XmlUtil {
	public static void saxXml(String path, SaxHandler saxHandler) {
		InputStream in = null;
		try {
			in = new FileInputStream(path);
			saxXml(in, saxHandler);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} finally {
			if (in != null) {
				try {
					in.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}

	public static void saxXml(InputStream in, SaxHandler saxHandler) {
		SAXParserFactory factory = SAXParserFactory.newInstance();
		try {
			SAXParser parser = factory.newSAXParser();
			parser.parse(in, saxHandler);
		} catch (Exception e) {
			e.printStackTrace();
		}
		saxHandler.resultCallback();
	}

	public static void domXml(String path, DomHandler domHandler) throws UtilException {
		File file = new File(path);
		InputStream in = null;
		try {
			in = FileUtil.getResourcAsStream(file);
			domXml(in, file, domHandler);
		} catch (UtilException e) {
			e.printStackTrace();
		} finally {
			if (in != null) {
				try {
					in.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}

	}

	/**
	 * dom解析xml
	 * 
	 * @param in 读取目标
	 * @param file 保存目标
	 * @param domHandler 处理器
	 * @throws UtilException 
	 */
	public static void domXml(InputStream in, File file, DomHandler domHandler) throws UtilException {
		SAXBuilder saxBuilder = new SAXBuilder();
		Document doc = null;
		try {
			doc = saxBuilder.build(in);
		} catch (JDOMException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		domHandler.resultCallback(doc);
		if (file != null) {
			Format format = Format.getCompactFormat();
			// 设置XML文件的缩进为4个空格
			format.setIndent("    ");
			// 设置XML文件的字符集为UTF-8
			format.setEncoding("UTF-8");
			// 将格式应用到输出流中
			XMLOutputter XMLOut = new XMLOutputter(format);
			// 将文档通过文件输出流生成xml文件
			try {
				XMLOut.output(doc, new FileOutputStream(file));
			} catch (FileNotFoundException e) {
				throw new UtilException("输出目录文件不存在",e);
			} catch (IOException e) {
				throw new UtilException("保存文件时IO异常",e);
			}
		}
	}
	public static void createXml(File file, DomHandler domHandler) throws UtilException {
		Document doc = new Document();
		domHandler.resultCallback(doc);
		if (file != null) {
			Format format = Format.getCompactFormat();
			// 设置XML文件的缩进为4个空格
			format.setIndent("    ");
			// 设置XML文件的字符集为UTF-8
			format.setEncoding("UTF-8");
			// 将格式应用到输出流中
			XMLOutputter XMLOut = new XMLOutputter(format);
			// 将文档通过文件输出流生成xml文件
			try {
				XMLOut.output(doc, new FileOutputStream(file));
			} catch (FileNotFoundException e) {
				throw new UtilException("输出目录文件不存在",e);
			} catch (IOException e) {
				throw new UtilException("保存文件时IO异常",e);
			}
		}
	}
	
}
